/**
 * All Rights Reserved By www.xandroid.cn
 */

package xfok.net.xandroid;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

/**
 * @author YangHua
 *
 * 2009-11-3 www.xandroid.cn
 */
public class Xandroid extends ListActivity {
    /** Called when the activity is first created. */
	private static String TAG="Xandroid";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        String path = intent.getStringExtra("xfok.net.xandroid.examples.Path");
        
        if (path == null) {
            path = "";
        }
        setListAdapter(new SimpleAdapter(this, getData(path),
                android.R.layout.simple_list_item_1, new String[] { "title" },
                new int[] { android.R.id.text1 }));
        getListView().setTextFilterEnabled(true);
        
    }
    
    protected List getData(String prefix) {
        List<Map> myData = new ArrayList<Map>();

        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory("Xandroid");

        PackageManager pm = getPackageManager();
        List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);
        if (null == list)
            return myData;
        
        int len = list.size();
        Log.i(TAG,"len="+len);
        
        for (int i = 0; i < len; i++) {
            ResolveInfo info = list.get(i);
            CharSequence labelSeq = info.loadLabel(pm);
            String label = labelSeq != null
                    ? labelSeq.toString()
                    : info.activityInfo.name;             
            Log.i(TAG,"label="+label);
            String ex=i>=9?"ex.0"+(i+1):"ex.00"+(i+1);
            if(i==99)ex="ex."+(i+1);
            label=ex +" : "+label;
            	addItem(myData, label, activityIntent(
            	info.activityInfo.applicationInfo.packageName,
            	info.activityInfo.name));
        }

        Collections.sort(myData, sDisplayNameComparator);
        
        return myData;
    }

    private final static Comparator<Map> sDisplayNameComparator = new Comparator<Map>() {
        private final Collator collator = Collator.getInstance();
        public int compare(Map map1, Map map2) {
            return collator.compare(map1.get("title"), map2.get("title"));
        }
    };

    protected Intent activityIntent(String pkg, String componentName) {
        Intent result = new Intent();
        Log.i(TAG, "pkg="+pkg);
        Log.i(TAG, "componentName="+componentName);
        result.setClassName(pkg, componentName);
        return result;
    }
    
    protected void addItem(List<Map> data, String name, Intent intent) {
        Map<String, Object> temp = new HashMap<String, Object>();
        Log.i(TAG,"addItem="+name);
        temp.put("title", name);
        temp.put("intent", intent);
        data.add(temp);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Map map = (Map) l.getItemAtPosition(position);
        Intent intent = (Intent) map.get("intent");
        String title=(String)map.get("title");
        Log.i(TAG,"title="+title);
        Log.i(TAG,"position="+position);
        startActivity(intent);
    }
}